'''
Copyright 2010 Indra Bagus <indra.bagus@gmail.com> 
    
Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

    http://www.apache.org/licenses/LICENSE-2.0 
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. 
'''
import getopt

class DistillerConfig(object):

    
    class INPUT_FORMAT_ENUM:
        C_ARRAY_1BYTE,C_ARRAY_2BYTE, C_ARRAY_4BYTE = range(3)

        
    class OUTPUT_FORMAT_ENUM:
        PLAINTEXT, HTML    = range(2)


    map_input_format = {'c_array_1byte':INPUT_FORMAT_ENUM.C_ARRAY_1BYTE,
                        'c_array_2byte':INPUT_FORMAT_ENUM.C_ARRAY_2BYTE,
                        'c_array_4byte':INPUT_FORMAT_ENUM.C_ARRAY_4BYTE}
    
    map_output_format = {'plaintext':OUTPUT_FORMAT_ENUM.PLAINTEXT,
                         'html'     :OUTPUT_FORMAT_ENUM.HTML}
        
    def __init__(self):
        self.output = str()
        self.output_format      = DistillerConfig.OUTPUT_FORMAT_ENUM.PLAINTEXT
        self.printpayload       = True
        self.inputformat        = DistillerConfig.INPUT_FORMAT_ENUM.C_ARRAY_1BYTE
        self.inputfile          = str()
        self.verbose            = True
        self.enable_crc_filter  = True


class ArgvParser(object):
    '''
    classdocs
    '''

    def __init__(self):
        pass
    
    def __get_str_help(self):
        retval =    'Usage: ofdma_distiller [OPTIONS...] [FILE...]\n'\
                    'Examples\n'\
                    '    app --output=file.output --printpayload'\
                    ' --inputformat=c_array_1byte --inputfile=input.dat'\
                    '\n'\
                    'DistillerConfig :\n\n'\
                    '-v, --version              Version\n'\
                    '-h, --help                 Help\n'\
                    '-o, --output=FILE          Output will generated into FILE\n'\
                    '-p, --printpayload         Enable payload MAC PDU printing in output file\n'\
                    '-i, --input=FILE           Input file\n'\
                    '    --inputformat=FORMAT   Format of input file\n'\
                    '    --outputformat=FORMAT  Format of input file\n'\
                    '    --verbose=[on/off]     Turn verbosity \'on\' or \'off\'\n'\
                    '    --with-crc-filter      Enable CRC Filter\n'\
                    '    --without-crc-filter   Disable CRC Filter(default)\n'\
                    '\n\n'
        retval += self.__get_str_input_format__()
        return retval
        
        
    def __get_str_version__(self):
        retval =    '0.1\n'\
                    'Author: Indra Bagus<indra.bagus@gmail.com>'
        return retval

    def __get_str_input_format__(self):
        retval =    'FORMAT is one of the following :\n\n'\
                    '    c_array_1byte       File contain c array format in byte as MAC PDU\n'\
                    '    c_array_2byte       File contain c array format in short as MAC PDU\n'\
                    '    c_array_4byte       File contain c array format in integer as MAC PDU\n'
        return retval
    
    
    def Parse(self,strinput=str()):
        '''
        Parse input argument string and
        returned the configuration
        '''
        retval = DistillerConfig()
        try:
            opts,args = getopt.getopt(strinput, 'o:pi:vh',
                                      ['help',
                                       'version',
                                       'output=',
                                       'printpayload',
                                       'input=',
                                       'inputformat=',
                                       'outputformat=',
                                       'verbose=',
                                       'with-crc-filter',
                                       'without-crc-filter'])
            if(len(opts) == 0):
                print self.__get_str_help()
                return None
            
            for o,a in opts:
                if o in ('-v','--version'):
                    print self.__get_str_version__()
                    return None
            
                elif o in ('-h', '--help'):
                    print self.__get_str_help()
                    return None
            
                if o in ('-o','--output'):
                    retval.output = a
            
                if o in ('-p','--printpayload'):
                    retval.printpayload = True
            
                if o == '--inputformat' :
                        retval.inputformat = DistillerConfig.map_input_format[a]
                        
                if o in ('-i','--input'):
                    retval.inputfile = a
            
                if o == '--verbose':
                    if(a == 'on'):
                        retval.verbose = True
                    elif(a=='off'):
                        retval.verbose = False
                    else:
                        raise getopt.GetoptError,'Verbosity value neither \'on\' or \'off\''
                    
                if o=='--with-crc-filter':
                    retval.enable_crc_filter = True
                    
                if o=='--without-crc-filter':
                    retval.enable_crc_filter = False
            
        except getopt.GetoptError,err:
            print self.__get_str_help()            
            raise NotImplementedError,err
        except KeyError:
            raise NotImplementedError,'error format \n'+self.__get_str_input_format__()
        
        return retval
        
        
        